<?xml version="1.0"?>
<OnTheFlyInstruction xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <InstanceId>Search - Get Schema</InstanceId>
  <CustomAppConfig />
  <Tags>
    <string>SharePoint Server</string>
  </Tags>
  <AdditionalAssemblies>
    <string>C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\14\ISAPI\Microsoft.Office.Server.dll</string>
  </AdditionalAssemblies>
  <Code><![CDATA[// ----------------------
// Serialize SharePoint search properties
//  administrator - 7/13/2013 
// v 0.0.1 - 2013-07-15 - Added additional assembly - mmachado
// ----------------------
  
using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Office.Server.Search.Administration;
using Microsoft.SharePoint;
using xSolon.Instructions.DTO;

namespace xSolon.Instructions.SPServer.Instructions
{
    public class ManagedPropertiesSetupInstruction : AbstractInstruction
    {
        public string SiteUrl = "http://localhost";

        
        /// <summary>

        /// Runs this instance.

        /// </summary>

        public override void Run()
        {
            SPServiceContext serviceContext = SPServiceContext.GetContext(new SPSite(SiteUrl));

            // Get the SearchApplicationProxy object from service context object

            SearchServiceApplicationProxy searchApplicationProxy = serviceContext.GetDefaultProxy(typeof(SearchServiceApplicationProxy)) as SearchServiceApplicationProxy;

            // Get the identifier for the search service application from the proxy class retrieved above.

            Guid applicationId = searchApplicationProxy.GetSearchServiceApplicationInfo().SearchServiceApplicationId;

            // Lastly, retreieve the SearchServiceApplication object for the current site.

            SearchServiceApplication searchApplication = SearchService.Service.SearchApplications.GetValue<SearchServiceApplication>(applicationId);

            //var table = GetFastList(searchApplication).ListToTable();

            // takes forever

            var table = GetMList(searchApplication).ListToTable();

            NotifyInformation(table.Rows.Count.ToString());

            table.AcceptChanges();

            ResultTable = table;
            
            var fileName = System.IO.Path.GetTempFileName();
            
            NotifyInformation("Saving table here: {0}", fileName);
            
            table.WriteXml(fileName);
        }

        private List<Dictionary<string, string>> GetFastList(SearchServiceApplication searchApplication)
        {
            var properties = searchApplication.GetManagedProperties();

            var list = new List<Dictionary<string, string>>();

            foreach (var prop in properties)
            {
                if (StopPending)
                {
                    break;
                }

                NotifyInformation(prop.Name);

                var itemDesc = prop.GetXMLValuesDictionary();

                list.Add(itemDesc);
            }

            return list;
        }

        private List<Dictionary<string, string>> GetMList(SearchServiceApplication searchApplication)
        {
            Schema sspSchema = new Schema(searchApplication);

            ManagedPropertyCollection properties1 = sspSchema.AllManagedProperties;

            var list = new List<Dictionary<string, string>>();

            var props = properties1.OfType<ManagedProperty>().Where(i => i.Name.StartsWith("Nidd")).ToList();

            foreach (var prop in props)
            {

                if (StopPending)
                {
                    break;
                }
                NotifyInformation(prop.Name);

                var itemDesc = prop.GetXMLValuesDictionary();

                var crawProps = prop.GetMappedCrawledProperties(10);

                var rawCProps = string.Empty;

                if (crawProps.Count > 0){
                    NotifyInformation("Found {0} crawled props", crawProps.Count);
                    rawCProps = crawProps.Aggregate("", (x, y) => string.Concat(x, ",", y.Name));
                }
                itemDesc.Add("CrawledProps", rawCProps);

                list.Add(itemDesc);
            }

            return list;
        }

        List<Dictionary<string, string>> GetCrawledList(SearchServiceApplication searchApplication)
        {
            Schema schema = new Schema(searchApplication);

            var list = new List<Dictionary<string, string>>();

            foreach (CrawledProperty prop in schema.QueryCrawledProperties(string.Empty, 1000, Guid.NewGuid(), string.Empty, true))
            {
                var itemDesc = prop.GetXMLValuesDictionary();

                list.Add(itemDesc);
            }

            return list;
        }

        void CreateProp(SearchServiceApplication searchApplication)
        {
            Schema schema = new Schema(searchApplication);

            var prop = schema.AllManagedProperties.Create("propName", ManagedDataType.Text);

            MappingCollection mappings = new MappingCollection();

            //foreach (CrawledProperty cprop in

            //         listBoxCrawledProperties.SelectedItems)

            //{

            //    mappings.Add(new Mapping

            //      (cprop.Propset, cprop.Name, cprop.VariantType, prop.PID));

            //}

            prop.SetMappings(mappings);
        }
    }
}]]></Code>
</OnTheFlyInstruction>